<template>
  <div class="BrowsingHistory">
    <van-nav-bar
        title="浏览记录"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
    />
    <van-tabs v-model:active="activeTab">
        <van-list
            v-model:loading="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
        >
          <van-swipe-cell  v-for="BrowsingHistory in users">
          <van-cell
              :title="BrowsingHistory.title"
              :label="BrowsingHistory.content"
              :is-link="false"
          >
          </van-cell>
            <template #left>
              <span class="item-header-title">{{BrowsingHistory.historyId}}</span>
              <span class="item-header-time">{{BrowsingHistory.createdAt}}</span>
            </template>
            <template #right>
              <van-button square type="danger" text="删除" class="delete-button" @click="delQuestion(BrowsingHistory.historyId)"/>
            </template>
          </van-swipe-cell>
        </van-list>
    </van-tabs>
  </div>
</template>

<script setup>
import {ref} from 'vue'
import {useRouter} from "vue-router";
import axios from "axios";
const router=useRouter();
const onClickLeft = () => {
  router.push("/my");
  // 返回按钮逻辑
};
const activeTab = ref(0);
const loading = ref(false);
const finished = ref(false);

const users = ref([
]);

const onLoad = () => {
  get('browsingHistory/findAll'
  ).then(
      (data) => {
        console.log(JSON.stringify(data))
        console.log("获取处理器的数据：" + JSON.stringify(data.data))

        // 加载状态结束
        loading.value = false;
        finished.value = true

        if (data.code == 0) {
          showToast("查询成功");
          users.value = data.data;// 初始化界面
        } else {
          showToast("查询失败");
        }


      })
      .catch(
          function () {
            alert("服务器错误")
          }
      )
};
import {showConfirmDialog, showToast} from 'vant';
import {get} from "../../util/request.js";
const  delQuestion = (historyId) => {

  console.log("根据id删除问题：" + historyId);
  // 弹出确认弹框
  showConfirmDialog({
    title: '删除问题',
    message:
        '确定删除该问题么？',
  })
      .then(() => {
        // on confirm
        // 发起ajax 请求
        // 综合方式
        get(
         'browsingHistory/deleteById',
          {
            historyId: historyId
        }).then(
            (data) => {
              console.log(JSON.stringify(data))
              if (data.code == 0) {
                showToast("删除成功")
                window.location.reload();
              } else {
                showToast("删除失败")
              }
            })
            .catch(
                function () {
                  alert("服务器错误")
                }
            )

      })
      .catch(() => {
        // on cancel

      });
}
</script>

<style scoped>
.delete-button {
  height: 100%;
}
</style>